package com.lhs.aopcache.processor;

import com.lhs.aopcache.AopCacheProcessor;
import com.lhs.aopcache.Cache;
import com.lhs.aopcache.base.CacheMetadata;
import com.lhs.aopcache.interceptor.AopCacheInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.beans.factory.InitializingBean;

public class AbstractAopCacheProcessor implements AopCacheProcessor, InitializingBean {

    protected static final String SPLIT_KEY = ",";

    private AopCacheInterceptor interceptor;

    public void setInterceptor(AopCacheInterceptor interceptor) {
        this.interceptor = interceptor;
    }


    public void afterPropertiesSet() throws Exception {
        interceptor.addProcessor(this);
    }

    public boolean preProcess(CacheMetadata metadata,
                              MethodInvocation invocation) {
        checkMetadata(metadata);
        return doPreProcess(metadata, invocation);
    }


    protected boolean doPreProcess(CacheMetadata metadata,
                                   MethodInvocation invocation) {
        return true;
    }


    protected void checkMetadata(CacheMetadata metadata) {
    }


    public void postProcess(CacheMetadata metadata, MethodInvocation invocation, Object result) {

    }


    public Object endProcessor(CacheMetadata metadata,
                               MethodInvocation invocation) {
        return null;
    }

    public Cache getAopCache() {
        return interceptor.getCache();
    }
}
